package day16;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class UserSystem {
    static Map<Integer,String> map1=new HashMap<>();
    static Map<String,Integer> map2=new HashMap<>();
    static Scanner sc=new Scanner(System.in);
    static Integer n=0;
    public static void main(String[] args) {
        One();
    }

    public static void One(){
        while (true){
        System.out.println("***XXX用户管理系统***");
        System.out.println("1.用户注册");
        System.out.println("2.用户登录");
        System.out.println("3.退出系统");
        System.out.println("*******************");
            System.out.print("请输入一级菜单对应的编号：");
            int n= sc.nextInt();
            switch (n){
                case 1:
                    Register();
                    break;
                case 2:
                    Login();
                    break;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("输入错误，请重新输入！");
                    continue;
            }
        }

    }

    public static void Register(){
        n++;
        System.out.print("请输入要注册的账号：");
        String account=sc.next();
        System.out.print("请输入密码：");
        int psssword=sc.nextInt();
        if (map2.containsKey(account)){
            System.out.println("该账号已存在,注册失败！");
        }else {
            map2.put(account,psssword);
            map1.put(n,account);
            System.out.println("注册成功！");
        }
    }

    public static void Login(){
        while (true){
            System.out.print("请输入账号：");
            String account=sc.next();
            System.out.print("请输入密码：");
            int password=sc.nextInt();
            if (map2.containsKey(account)){
                int getPassword=map2.get(account);
                if (getPassword==password){
                    System.out.println("***"+account+"登录成功***");
                    Two();
                    break;
                }else {
                    System.out.println("密码错误,请重新输入！");
                    continue;
                }
            }else {
                System.out.println("账号不存在，请重新输入！");
                continue;
            }
        }

    }

    public static void Two(){
        System.out.println("1.展示用户");
        System.out.println("2.修改密码");
        System.out.println("3.删除用户");
        System.out.println("4.用户退出");
        System.out.println("********************");
        one:
        while (true){
        System.out.print("请输入二级菜单对应的编号：");
        int n=sc.nextInt();
            switch (n){
                case 1:
                    ShowUser();
                    break;
                case 2:
                    ChangePassword();
                    break;
                case 3:
                    DeleteUser();
                    break;
                case 4:
                    break one;
                default:
                    System.out.println("输入错误，请重新输入!");
                    continue;
            }
        }

    }

    public static void ShowUser(){
        for(Integer k:map1.keySet()){
            String v=map1.get(k);
            Integer i=map2.get(v);
            System.out.println("用户编号："+k+"，用户账号："+v+"，用户密码:"+i);
        }
    }
    public static void ChangePassword(){
            System.out.print("请输入用户编号：");
            int i=sc.nextInt();
            if (map1.containsKey(i)){
                String s=map1.get(i);
                int password=map2.get(s);
                System.out.print("请输入旧密码：");
                int oldPassword=sc.nextInt();
                if (oldPassword==password){
                    System.out.print("请输入新密码：");
                    int newPassword= sc.nextInt();
                    map2.put(s,newPassword);
                    System.out.println("密码修改成功！");
                }else {
                    System.out.println("旧密码输入错误！");
                }
            }else {
                System.out.println("用户编号不存在，修改密码失败!");
            }
    }
    public static void DeleteUser(){
        System.out.print("请输入用户编号：");
        int i=sc.nextInt();
        if (map1.containsKey(i)){
            String s=map1.get(i);
            map1.remove(i);
            map2.remove(s);
            System.out.println("用户"+s+"删除成功！");
        }else {
            System.out.println("用户编号不存在，删除失败！");
        }

    }
}

